//
//  CarpoolPublishViewController.m
//  cityStar
//
//  Created by Hancong on 15/2/11.
//  Copyright (c) 2015年 tiny. All rights reserved.
//

#import "CarpoolPublishViewController.h"
#import "selectDistrict.h"
#import "UIColor+HexColors.h"
#import "RJPlaceHolderTextView.h"
#import "CarpoolingViewController.h"

//#import <CoreBluetooth/CoreBluetooth.h>
@interface CarpoolPublishViewController ()<UIPickerViewDelegate,UIPickerViewDataSource,UITextFieldDelegate,UITextViewDelegate>

@property (nonatomic,strong) NSArray *arrayName;
@property (nonatomic,strong) NSArray *arrayid;
@property (nonatomic,strong) NSString *strstartAddressID;
@property (nonatomic,strong) NSString *carpoolType;
@property (nonatomic,strong) NSString *payType;

@end

@implementation CarpoolPublishViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    [self setUpViews];
    [self addBackButton];
    [self addRightButtonWithTitle:@"发布须知" actionBlock:^{
        
    }];
}

-(void)setUpViews
{
    self.title = @"拼车";
    self.carpoolType = @"0";
    self.payType = @"0";

    self.scrollerView.contentSize = CGSizeMake(320, 920);
    self.arrayName = [[NSArray alloc]init];
    self.arrayid = [[NSArray alloc]init];
    self.textDetail.placeholder = @"填写对拼车人的一些要求或者其他说明";
    self.textDetail.placeholderColor = [UIColor colorWithWhite:0.797 alpha:1.000];
    self.arrayName  = [selectDistrict shardDistrictInfo].DistrictNameArray;
    self.arrayid =[selectDistrict shardDistrictInfo].DistrictIdArray;
    self.strstartAddressID = [NSString stringWithFormat:@"%@",self.arrayid[0]];

    UIPickerView *typePickerView = [[UIPickerView alloc]init];
    typePickerView.delegate = self;
    typePickerView.tag = 0;
    self.fieldType.inputView = typePickerView;
    
    self.fieldStartTime.datePicker.datePickerMode = UIDatePickerModeDateAndTime;
    
    UIPickerView *addressPickerView = [[UIPickerView alloc]init];
    addressPickerView.delegate = self;
    addressPickerView.tag = 1;
    self.fieldStartAddress.inputView = addressPickerView;
    
    UIPickerView *payPickerView = [[UIPickerView alloc]init];
    payPickerView.delegate = self;
    payPickerView.tag = 2;
    self.fieldPayType.inputView = payPickerView;
    
    UIPickerView *typeCarPickerView = [[UIPickerView alloc]init];
    typeCarPickerView.delegate = self;
    typeCarPickerView.tag = 3;
    self.fieldModel.inputView = typeCarPickerView;

    
    UIToolbar * topView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
    [topView setBarStyle:UIBarStyleDefault];
    
    UIBarButtonItem * btnSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    
    UIBarButtonItem * doneButton = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(dismissKeyBoard)];
    
    NSArray * buttonsArray = [NSArray arrayWithObjects:btnSpace,doneButton,nil];
    
    [topView setItems:buttonsArray];
    [self.fieldModel setInputAccessoryView:topView];
    [self.fieldType setInputAccessoryView:topView];
    [self.fieldStartTime setInputAccessoryView:topView];
    [self.fieldStartAddress setInputAccessoryView:topView];
    [self.fieldPayType setInputAccessoryView:topView];

}

-(void)dismissKeyBoard
{
    [self.fieldModel resignFirstResponder];
    [self.fieldType resignFirstResponder];
    [self.fieldStartTime resignFirstResponder];
    [self.fieldStartAddress resignFirstResponder];
    [self.fieldPayType resignFirstResponder];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    switch (textField.tag) {
        case 2:
        {
            textField.text = @"上下班拼车";
        }
            break;
        case 3:
        {
            textField.text = @"经济型";
        }
            break;
        case 5:
        {
            textField.text = @"城市之星 星币";
        }
            break;
        case 6:
        {
            [UIView animateWithDuration:0.3f animations:^{
                self.scrollerView.contentOffset = CGPointMake(0, 100);
            }];
        }
            break;
        case 7:
        {
            [UIView animateWithDuration:0.3f animations:^{
                self.scrollerView.contentOffset = CGPointMake(0, 150);
            }];
            //            textField.text = self.arrayName[0];
        }
            break;
        case 8:
        {
            [UIView animateWithDuration:0.3f animations:^{
                self.scrollerView.contentOffset = CGPointMake(0, 200);
//                textField.text = @"押一付二";
            }];
            
        }
            break;
        case 9:
        {
            [UIView animateWithDuration:0.3f animations:^{
                self.scrollerView.contentOffset = CGPointMake(0, 250);
//                textField.text =  [NSDate date];
            }];
        }
            break;
        case 10:
        {
            [UIView animateWithDuration:0.3f animations:^{
                self.scrollerView.contentOffset = CGPointMake(0, 300);
                textField.text = self.arrayName[0];
            }];
        }
        case 11:
        {
            [UIView animateWithDuration:0.3f animations:^{
                self.scrollerView.contentOffset = CGPointMake(0, 350);
            }];
        }
        case 12:
        {
            [UIView animateWithDuration:0.3f animations:^{
                self.scrollerView.contentOffset = CGPointMake(0, 400);
            }];
        }
            break;
            
        default:
            break;
    }
}

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}

#pragma mark - pickerViewDelegate
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
        return 1;
}

-(NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (pickerView.tag == 0) {
    return 3;
    }else if(pickerView.tag == 1){
        return self.arrayName.count;
    }else if(pickerView.tag == 2){
        return 2;
    }else{
        return 3;
    }
}

-(NSString*) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (pickerView.tag == 0) {
    if (row == 0) {
        return @"上下班拼车";
    }else if (row == 1){
        return @"单程旅行拼车";
    }else
    {
        return @"往返双程旅行拼车";
    }
    }else if(pickerView.tag == 1){
        return self.arrayName[row];
    }else if(pickerView.tag == 2){
        if (row == 0) {
            return @"城市之星 星币";
        }else{
            return @"现金结算";
        }
    }else{
        if (row == 0) {
            return @"经济型";
        }else if (row == 1){
            return @"舒适型";
        }else{
            return @"豪华型";
        }
    }
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (pickerView.tag == 0) {
    if (row == 0) {
        self.fieldType.text = @"上下班拼车";
        self.carpoolType = @"0";
    }else if (row == 1){
        self.fieldType.text = @"单程旅行拼车";
        self.carpoolType = @"1";
    }else
    {
        self.fieldType.text = @"往返双程旅行拼车";
        self.carpoolType = @"2";
    }
    }else if(pickerView.tag == 1){
        NSString *strname = [NSString stringWithFormat:@"%@",self.arrayName[row]];
        self.strstartAddressID = [NSString stringWithFormat:@"%@",self.arrayid[row]];
        self.fieldStartAddress.text = strname;
    }else if(pickerView.tag == 2){
        if (row == 0) {
            self.fieldPayType.text = @"城市之星 星币";
            self.payType = @"0";
        }else{
            self.fieldPayType.text = @"现金结算";
            self.payType = @"1";
        }
    }else{
        if (row == 0) {
            self.fieldModel.text = @"经济型";
        }else if(row == 1) {
            self.fieldModel.text = @"舒适型";
        }else{
            self.fieldModel.text = @"豪华型";
        }
    }
}

#pragma mark - Actions
-(IBAction)btnPublish_Touch:(id)sender
{
    NSString *errMsg;
    if (self.fieldTitle.text.length == 0) {
        errMsg = @"请输入标题";
    }else if (self.fieldType.text.length == 0){
        errMsg = @"请选择类型";
    }else if (self.fieldModel.text.length == 0){
        errMsg = @"请输入拼车车型";
    }else if (self.fieldNumPeople.text.length == 0){
        errMsg = @"请输入拼车人数";
    }else if (self.fieldFromAddress.text.length == 0){
        errMsg = @"请输入起点";
    }else if (self.fieldToAddress.text.length == 0){
        errMsg = @"请输入终点";
    }else if (self.fieldAfterAddress.text.length == 0){
        errMsg = @"请输入途径";
    }else if (self.fieldStartTime.text.length == 0){
        errMsg = @"请选择时间";
    }else if (self.fieldStartAddress.text.length == 0){
        errMsg = @"请选择出发地点";
    }else if (self.fieldContact.text.length == 0){
        errMsg = @"请输入联系人";
    }else if (self.fieldContactPhone.text.length == 0){
        errMsg = @"请输入联系电话";
    }else if (self.textDetail.text.length == 0){
        errMsg = @"请输入详情描述";
    }
    if (errMsg) {
        BossSystemShowAlertViewWithTitle(errMsg)
        return;
    }
    
    long time;
    NSDateFormatter *format=[[NSDateFormatter alloc] init];
    [format setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSDate *fromdate=[format dateFromString:self.fieldStartTime.text];
    time = (long)[fromdate timeIntervalSince1970];
    
    NSString *cityID = [[NSUserDefaults standardUserDefaults]objectForKey:@"city_code"];
    [[NSUserDefaults standardUserDefaults]synchronize];
    
    NSDictionary *parameters = @{@"title":self.fieldTitle.text,
                                 @"carpool_type":self.carpoolType,
                                 @"people":self.fieldNumPeople.text,
                                 @"start":self.fieldFromAddress.text,
                                 @"end":self.fieldToAddress.text,
                                 @"way":self.fieldAfterAddress.text,
                                 @"departure":[NSString stringWithFormat:@"%li",time],
                                 @"district":self.strstartAddressID,
                                 @"district_name":self.fieldStartAddress.text,
                                 @"contact":self.fieldContact.text,
                                 @"mobile":self.fieldContactPhone.text,
                                 @"description":self.textDetail.text,
                                 @"pay":self.payType,
                                 @"city":cityID?cityID:@"122"
                                     };
    [HttpRequestManager postMethod:KInterface_PublishCarpool parameters:parameters completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
        DLog(@"%@",response);
        if (success) {
            [SVProgressHUD showSuccessWithStatus:@"发布成功"];
            CarpoolingViewController *carpoolingVC = [[CarpoolingViewController alloc]init];
            [self.navigationController pushViewController:carpoolingVC animated:YES];
        }else{
            [SVProgressHUD showSuccessWithStatus:errMsg];
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
